//
//  AddNewAddressController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/1.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "AddNewAddressController.h"
#import "HDAreaChoiceView.h"

@interface AddNewAddressController ()<BaseViewDelegate>

@property (strong, nonatomic)HDAreaChoiceView *areaChoiceView;
@property (nonatomic , copy) NSString * region_name;
@property (nonatomic , copy) NSString * region_id;
@property (nonatomic , assign) NSInteger is_default;
@end

@implementation AddNewAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)setNavi
{
    self.title = @"收货地址";
    [self.rightBtn setTitle:@"保存" forState:(UIControlStateNormal)];
}

-(void)viewWillAppear:(BOOL)animated
{
    if (self.model.address_id.length > 0) {
        self.nameText.text = self.model.consignee;
        self.region_name = self.model.region_name;
        self.region_id = [NSString stringWithFormat:@"%ld",self.model.region_id];
        self.detailText.text = self.model.detail_address;
        self.teleText.text = self.model.telephone;
        if (self.model.is_default == 1) {
            self.defaultSwitch.on = YES;
        }else
        {
            self.defaultSwitch.on = NO;
        }
    }
}

-(void)rightBtnAction:(UIButton *)sender
{
    if (self.nameText.text.length > 0 && self.teleText.text.length > 0 && self.region_name.length > 0 && self.detailText.text.length > 0) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        NSMutableDictionary * dict = @{@"token":self.userToken,@"consignee":self.nameText.text,@"telephone":self.teleText.text,@"region_id":self.region_id,@"region_name":self.region_name,@"detail_address":self.detailText.text,@"is_default":[NSString stringWithFormat:@"%ld",self.is_default]}.mutableCopy;
        if (self.model.address_id.length > 0) {
            [dict setObject:self.model.address_id forKey:@"address_id"];
        }
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@address/add-edit-address",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (isSuccess) {
                [MBProgressHUD showSuccess:@"添加成功" toView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else
    {
        [MBProgressHUD showError:@"请完善信息" toView:self.view];
    }
}

-(void)setUI
{
    self.view.backgroundColor = Color(245, 245, 245);
    self.is_default = 1;
}

- (IBAction)changeArea:(UIButton *)sender {
    [self.view endEditing:YES];
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeBottom;
    [self.zh_popupController presentContentView:self.areaChoiceView duration:0.2 springAnimated:NO];
}

- (void)viewActionWithView:(UIView *)view tag:(NSInteger)tag data:(id)data {
    if ([view isKindOfClass:[HDAreaChoiceView class]]) {
        [self.zh_popupController dismiss];
        self.areaChoiceView = nil;
        
        self.region_id = [data strForKey:@"region_id"];
        self.region_name = [data strForKey:@"region_name"];
        [self.areaBtn setTitle:[data strForKey:@"region_name"] forState:UIControlStateNormal];
    }
}

- (IBAction)isDefault:(UISwitch *)sender {
    self.is_default = sender.on;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (HDAreaChoiceView *)areaChoiceView {
    if (!_areaChoiceView) {
        _areaChoiceView = [[HDAreaChoiceView alloc] initWithFrame:CGRectMake(0, kScreenHeight/2.0, kScreenWidth, kScreenHeight/2.0)];
        _areaChoiceView.delegate = self;
    }
    return _areaChoiceView;
}


@end
